<?php

class CompleteAction extends Action {

    //put your code here
    public function index() {
        $uid = $_SESSION['uid'];
        $db = M('atten');
        $arr = $db->where(array('uid' => $uid))->select();
        if ($arr) {
            foreach ($arr as $k => $v) {
                $db = D('ImgView');
                $data = $db->where('kid=' . $v['kid'])->limit(10)->order('id desc')->select();
                if ($data) {
                    $img[] = $data;
                }
            }
            $num = 0;
            foreach ($img as $k => $v) {
                foreach ($v as $z) {
                    $img[$num] = $z;
                    $db = M('good');
                    $n = $db->where(array('mid' => $z['id']))->count();
                    if ($n) {
                        $img[$num]['num'] = $n;
                    } else {
                        $img[$num]['num'] = '';
                    }
                    $num++;
                }
            }
//            echo "<pre>";
//            print_r($img);die;
            $_SESSION['start'] = 0;
            $this->assign('img', $img);
        }
        $db = M('user');
        $user = $db->field('uname,thump,inum,uid,num')->where('uid=' . $uid)->find();
        $this->assign('user', $user);
        $this->display();
    }

    public function ajax() {
        $num = $_POST['num'] * 10;
        $uid = $_SESSION['uid'];
        $db = M('atten');
        $ar = $db->where(array('uid' => $uid))->select();
        $db = D('ImgView');
        if ($ar) {
            foreach ($ar as $k => $v) {
                $allnum = $db->where(array('kid' => $v['kid']))->count();
                if ($num < $allnum) {
                    if ($allnum - $num >= 10) {
                        $arr[] = $db->where(array('kid' => $v['kid']))->limit("$num,10")->order('id desc')->select();
                    } else if (($allnum - $num) < 10) {
                        $snum = $allnum - $num;
                        $data = $db->where(array('kid' => $v['kid']))->limit("$num, $snum")->order('id desc')->select();
                        if ($data) {
                            $arr[] = $data;
                        }
                    }
                }
            }
            $num = 0;
            foreach ($arr as $k => $v) {
                if ($v) {
                    foreach ($v as $z) {
                        $arr[$num] = $z;
                        $db = M('good');
                        $n = $db->where(array('mid' => $z['id']))->count();
                        if ($n) {
                            $arr[$num]['num'] = $n;
                        } else {
                            $arr[$num]['num'] = '';
                        }
                        $db = D('CommentView');
                        $sort = $db->where("mid=" . $z['id'])->order("id asc")->select();
                        if ($sort) {
                            $arr[$num]['sort'] = $sort;
                        }
                        $num++;
                    }
                }
            }
        }
        if ($arr) {
            echo json_encode($arr);
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //关注的画板
    public function attehua() {
        $uid = $_SESSION['uid'];
        $db = M('atten');
        $ar = $db->where(array('uid' => $uid))->limit(10)->order('id desc')->select();
        if ($ar) {
            foreach ($ar as $v) {
                $db = D('SkedchpatView');
                $data = $db->where(array('kid' => $v['kid']))->find();
                if ($data) {
                    $arr[] = $data;
                }
            }
            $this->assign('hua', $arr);
        }
        $db = M('user');
        $user = $db->field('uname,thump,inum,uid,num')->where('uid=' . $uid)->find();
        $this->assign('user', $user);
        $this->display();
    }

    public function ajaxhua() {
        $num = $_POST['num'] * 10;
        $uid = $_SESSION['uid'];
        $db = M('atten');
        $ar = $db->where(array('uid' => $uid))->limit("$num ,10")->order('id desc')->select();
        if ($ar) {
            foreach ($ar as $k => $v) {
                $db = D('SkedchpatView');
                $arr[$k] = $db->where(array('kid' => $v['kid']))->find();
                $db = M('img');
                $img = $db->where('sid = ' . $v['kid'])->limit(9)->select();
                $arr[$k]['img'] = $img;
            }
            echo json_encode($arr);
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //验证画板名字是否已存在
    public function vername() {
        $name = htmlspecialchars($_POST['name']);
        $pid = $_SESSION['uid'];
        $db = M('skedchpat');

        $arr = $db->where(array('pid' => $pid, 'name' => $name))->find();
        if ($arr) {
            echo json_encode(array('num' => 0));
        } else {
            echo json_encode(array('num' => 1));
        }
    }

    //添加画布
    public function addhuab() {
        $data = array(
            'name' => htmlspecialchars($_POST['name']),
            'sid' => $_POST['sid'],
            'pid' => $_SESSION['uid']
        );
        $db = M('skedchpat');
        $kid = $db->add($data);
        if ($kid) {
            $db = M('user');
            $db->where('uid=' . $_SESSION['uid'])->setInc('num');
            $this->redirect('showhuab', array('kid' => $kid));
        } else {
            $this->error('新增失败');
        }
    }

    //异步添加画布
    public function ajaxhuaban() {
        $data = array(
            'name' => htmlspecialchars($_POST['name']),
            'pid' => $_SESSION['uid']
        );
        $db = M('skedchpat');
        if ($db->where("name='{$_POST['name']}'")->find()) {
            echo json_encode(array('kid' => 0));
            die;
        }
        $kid = $db->add($data);
        if ($kid) {
            $db = M('user');
            $db->where('uid=' . $_SESSION['uid'])->setInc('num');
            echo json_encode(array('kid' => $kid));
        } else {
            echo json_encode(array('kid' => 0));
        }
    }

    //显示画布
    public function showhuab() {
        $kid = $_GET['kid'];
        $db = M('skedchpat');
        $arr = $db->where(array('kid' => $kid))->find();
        if ($arr['sid'] != 0) {
            $db = D('SkedchpatView');
            $huab = $db->where(array('kid' => $kid))->find();
        } else {
            $user = $db->table('hua_user')->field('thump,uname,uid')->where(array('uid' => $arr['pid']))->find();
            $huab = array_merge($arr, $user);
        }

        $this->assign('huab', $huab);
        $uid = $huab['uid'];
        $db = M('skedchpat');
        $arr = $db->where(array('pid' => $uid, 'kid' => array('neq', $kid)))->select();
        $this->assign('other', $arr);
        // $img = $db->table('hua_img')->where(array('sid' => $kid))->limit(4)->select();
        //$this->assign('img', $img);
        $imgs = $db->table('hua_img')->where(array('sid' => $kid))->limit(10)->order('id desc')->select();
        foreach ($imgs as $k => $v) {
            $db = M('good');
            $num = $db->where(array('mid' => $v['id']))->count();
            if ($num) {
                $imgs[$k]['num'] = $num;
            } else {
                $imgs[$k]['num'] = '';
            }
        }
        $this->assign('imgs', $imgs);
        $this->display();
    }

    //给画布类别
    public function addsid() {
        $data = array(
            'sid' => (int) $_POST['sid'],
            'kid' => (int) $_POST['kid']
        );
        $db = M('skedchpat');
        if ($db->save($data)) {
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //添加图片
    public function addimg() {
        $_POST['pid'] = $_SESSION['uid'];
        $_POST['addtime'] = time();
        $db = M('img');
        if ($db->add($_POST)) {
            $db = M('user');
            $db->where('uid=' . $_SESSION['uid'])->setInc('inum');
            $db = M('skedchpat');
            $db->where('kid=' . $_POST['sid'])->setInc('num');
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    public function rmimg() {
        $adres = $_POST['adres'];
        $imgsrc = './static/' . basename(dirname($adres)) . '/' . basename($adres);
        $jp = strrchr($adres, '.');
        $filet = date("ymdH", time());
        $file = './static/' . $filet;
        is_dir($file) || mkdir($file, 0777, true);
        if (copy($imgsrc, $file . '/' . time() . $jp)) {
            $_POST['adres'] = dirname(__APP__) . '/static/' . $filet . '/' . time() . $jp;
        } else {
            echo json_encode(array('num' => 0));
            die;
        }
        $_POST['pid'] = $_SESSION['uid'];
        $_POST['addtime'] = time();
        $db = M('img');
        if ($db->add($_POST)) {
            $db = M('user');
            $db->where('uid=' . $_SESSION['uid'])->setInc('inum');
            $db = M('skedchpat');
            $db->where('kid=' . $_POST['sid'])->setInc('num');
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //展示图片
    public function showimg() {
        $db = D('ImgView');
        $arr = $db->where(array('id' => $_GET['id']))->find();
        $arr['knum'] = $arr['num'];
        $array = $db->where(array('kid' => $arr['kid']))->select();
        foreach ($array as $k => $v) {
            if ($arr['id'] == $v['id']) {
                $index = $k + 1;
            }
        }
        $this->assign('index', $index);
        if (!isset($_SESSION['uid'])) {
            $arr['atten'] = 0;
        } else {
            $db = M('atten');
            $uid = $_SESSION['uid'];
            if ($db->where(array('uid' => $uid, 'kid' => $arr['kid']))->find()) {
                $arr['atten'] = 1;
            } else {
                $arr['atten'] = 0;
            }
        }
        $db = M('good');
        $num = $db->where(array('mid' => (int) $_GET['id']))->count();
        if ($num) {
            $arr['num'] = $num;
            $goodData = $db->where(array('mid' => (int) $_GET['id']))->order('gid desc')->select();
            foreach ($goodData as $k => $v) {
                $thump = $db->table('hua_user')->field('thump')->where('uid=' . $v['uid'])->find();
                $goodData[$k]['thump'] = $thump['thump'];
            }
            $this->assign('goodData', $goodData);
        } else {
            $arr['num'] = '';
        }
        $this->assign('img', $arr);
        $uid = $arr['uid'];
        $kid = $arr['kid'];
        $db = M('img');
        $img = $db->where(array('sid' => $kid, 'pid' => $uid))->limit(9)->select();
        $this->assign('kimg', $img);
        $_SESSION['start'] = 1;
        $this->display();
    }

    //按下左右显示图片
    public function keydown() {
        $id = (int) $_GET['id'];
        $db = M('img');
        $img = $db->where('id=' . $id)->find();
        $imgs = $db->where('sid=' . $img['sid'])->select();
        $main = $_GET['main'];
        foreach ($imgs as $k => $v) {
            if ($v['id'] == $id) {
                $num = $k;
            }
        }
        if ($main == 'right') {
            if (isset($imgs[$num + 1])) {
                echo json_encode(array('id' => $imgs[$num + 1]['id']));
                die;
            }
        } else if ($main == 'left') {
            if (isset($imgs[$num - 1])) {
                echo json_encode(array('id' => $imgs[$num - 1]['id']));
                die;
            }
        }
        echo json_encode(array('num' => 1));
        die;
    }

    //用户
    public function showuser() {
        $uid = isset($_GET['uid']) ? $_GET['uid'] : $_SESSION['uid'];
        $db = M('user');
        $user = $db->where(array('uid' => $uid))->find();
        $count = $db->table('hua_good')->where('uid=' . $uid)->count();
        $user['gnum'] = $count;
        $this->assign('user', $user);
        $db = M('skedchpat');
        $sked = $db->where(array('pid' => $uid))->select();
        foreach ($sked as $k => $v) {
            if (!isset($_SESSION['uid'])) {
                $sked[$k]['atten'] = 0;
            } else {
                $db = M('atten');
                $uid = $_SESSION['uid'];
                if ($db->where(array('uid' => $uid, 'kid' => $v['kid']))->find()) {
                    $sked[$k]['atten'] = 1;
                } else {
                    $sked[$k]['atten'] = 0;
                }
            }
        }
        $this->assign('sked', $sked);
        $this->display();
    }

    //编辑画板
    public function geit() {
        $kid = (int) $_GET['kid'];
        $db = M('skedchpat');
        $arr = $db->where(array('kid' => $kid))->find();
        if ($arr['sid'] != 0) {
            $db = D('SkedchpatView');
            $arr = $db->where(array('kid' => $kid))->find();
        }
        $this->assign('data', $arr);
        $this->display();
    }

    //修改画板内容
    public function geithua() {
        $kid = $_GET['kid'];
        $db = M('skedchpat');
        if ($db->where(array('kid' => $kid))->save($_POST)) {
            $this->redirect('showhuab', array('kid' => $kid));
        }
    }

    //编辑图片
    public function editimg() {
        $id = (int) $_GET['id'];
        $db = D('ImgView');
        $arr = $db->where(array('id' => $id))->find();
        $this->assign('data', $arr);
        $this->display();
    }

    //保持编辑图片
    public function saveImg() {
        $id = (int) $_GET['id'];
        $sid = (int) $_POST['sid'];
        $data = array(
            'sid' => $sid,
            'id' => $id,
            'mesa' => htmlspecialchars($_POST['mesa']),
        );
        $db = M('img');
        $arr = $db->where('id=' . $id)->find();
        $db->save($data);
        $db = M('skedchpat');
        $db->where('kid=' . $arr['sid'])->setDec('num');
        $db->where('kid=' . $sid)->setInc('num');
        $this->redirect('showimg', array('id' => $id));
    }

    //删除画板
    public function delhua() {
        $kid = $_POST['kid'];
        $db = M('skedchpat');
        $stop = true;
        $arr = $db->where('kid = ' . $kid)->find();
        if (!$db->where('kid = ' . $kid)->delete()) {
            $stop = false;
        } else {
            $db = M('img');
            $imgs = $db->where('sid = ' . $kid)->select();
            if (!$db->where('sid = ' . $kid)->delete()) {
                $stop = false;
            }
        }
        if (stop) {
            foreach ($imgs as $v) {
                $file = './static/' . basename(dirname($v['adres'])) . '/' . basename($v['adres']);
                unlink($file);
            }
            $db = M('user');
            $db->where('uid=' . $_SESSION['uid'])->setDec('num');
            $db->where('uid=' . $_SESSION['uid'])->setDec('inum', $arr['num']);
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //删除图片
    public function delimg() {
        $id = (int) $_POST['id'];
        $db = M('img');
        $arr = $db->where(array('id' => $id))->find();
        $file = './static/' . basename(dirname($arr['adres'])) . '/' . basename($arr['adres']);
        if (is_file($file)) {
            if (unlink($file)) {
                if ($db->where(array('id' => $id))->delete()) {
                    $db = M('user');
                    $db->where('uid=' . $_SESSION['uid'])->setDec('inum');
                    $db = M('skedchpat');
                    $db->where('kid=' . $arr['sid'])->setDec('num');
                    $db->table('hua_commont')->where(array('mid' => $id))->delete();
                    echo json_encode(array('num' => 1, 'sid' => $arr['sid']));
                }
            }
        }
    }

    //关注画板
    public function atten() {
        $kid = (int) $_POST['kid'];
        $uid = $_SESSION['uid'];
        $data = array(
            'uid' => $uid,
            'kid' => $kid
        );
        $db = M('atten');
        if ($db->where($data)->find()) {
            echo json_encode(array('num' => 1));
            die;
        }
        if ($db->add($data)) {
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //取消关注
    public function rel() {
        $kid = (int) $_POST['kid'];
        $uid = $_SESSION['uid'];
        $db = M('atten');
        if ($db->where(array('kid' => $kid, 'uid' => $uid))->delete()) {
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //用户采集的画
    public function userimg() {
        $uid = (int) $_GET['uid'];
        $db = M('user');
        $user = $db->where(array('uid' => $uid))->find();
        $count = $db->table('hua_good')->where('uid=' . $uid)->count();
        $user['gnum'] = $count;
        $this->assign('user', $user);
        $db = D('ImgView');
        $img = $db->where('uid=' . $uid)->limit(10)->order('id desc')->select();
        foreach ($img as $k => $v) {
            $db = M('good');
            $n = $db->where(array('mid' => $v['id']))->count();
            if ($n) {
                $img[$k]['num'] = $n;
            } else {
                $img[$k]['num'] = '';
            }
            $num++;
        }
        $this->assign('img', $img);
        $this->display();
    }

    public function ajaxuser() {
        $num = $_POST['num'] * 10;
        $uid = $_POST['uid'];

        $db = D('ImgView');
        $arr = $db->where(array('uid' => $uid))->limit("$num,10")->order('id desc')->select();
        foreach ($arr as $k => $v) {
            $db = M('good');
            $n = $db->where(array('mid' => $v['id']))->count();
            if ($n) {
                $arr[$k]['num'] = $n;
            } else {
                $arr[$k]['num'] = '';
            }
            $db = D('CommentView');
            $sort = $db->where("mid=" . $v['id'])->order("id asc")->select();
            if ($sort) {
                $arr[$k]['sort'] = $sort;
            }
        }
        if ($arr) {
            echo json_encode($arr);
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //用户采集的画
    public function good() {
        $uid = (int) $_GET['uid'];
        $db = M('user');
        $user = $db->where(array('uid' => $uid))->find();
        $count = $db->table('hua_good')->where('uid=' . $uid)->count();
        $user['gnum'] = $count;
        $this->assign('user', $user);
        $db = D('GoodView');
        $img = $db->where('good.uid=' . $uid)->limit('10')->order('gid desc')->select();
        foreach ($img as $k => $v) {
            $db = M('good');
            $n = $db->where(array('mid' => $v['id']))->count();
            if ($n) {
                $img[$k]['num'] = $n;
            } else {
                $img[$k]['num'] = '';
            }
            $num++;
        }
        $this->assign('img', $img);
        $this->display();
    }

    public function ajaxgood() {
        $num = $_POST['num'] * 10;
        $uid = $_POST['uid'];

        $db = D('GoodView');
        $arr = $db->where(array('good.uid' => $uid))->limit("$num,10")->order('id desc')->select();
        foreach ($arr as $k => $v) {
            $db = M('good');
            $n = $db->where(array('mid' => $v['id']))->count();
            if ($n) {
                $arr[$k]['num'] = $n;
            } else {
                $arr[$k]['num'] = '';
            }
            $db = D('CommentView');
            $sort = $db->where("mid=" . $v['id'])->order("id asc")->select();
            if ($sort) {
                $arr[$k]['sort'] = $sort;
            }
        }
        if ($arr) {
            echo json_encode($arr);
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //获取原图
    public function bigimg() {
        $id = (int) $_POST['id'];
        $db = M('img');
        $img = $db->field('adres')->where('id=' . $id)->find();
        echo json_encode($img);
    }

}

?>
